package com.snimay.login;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.snimay.app.user.vo.User;

@Aspect
@Component
@Scope("session")
@RequestMapping("snimay")
public class HttpAspect {
	private User user;
	private Map  processurl;
	private SessionFactory usersessionFactory;
	private Date	startTime;
	private Date	overTime;
	private int i=0;
	@Pointcut("execution(* com.snimay.app..*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void permisson() {}
	
	/**
	 * @Description: TODO
	 * @author     : xxy
	 * @param pjp
	 * @return
	 * @throws Throwable
	 * @throws
	 */
	@Around("permisson()")
	public Object Interceptor(ProceedingJoinPoint pjp) throws Throwable{
		ServletRequestAttributes requestAttributes 	= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request 					= requestAttributes.getRequest();
		String	path								= request.getServletPath();
		String	method								= request.getMethod();
		Object	obj									;
		if(path.equals("/login")) {
			System.out.println("fd");
			obj	=	pjp.proceed(pjp.getArgs());
		}else {
			/*Method mt 				= pjp.getTarget().getClass().getMethod("setSeesionFactory", SessionFactory.class);
			if(mt!=null)mt.invoke(pjp.getTarget(), this.usersessionFactory);
			if(this.user==null) {
				ToWeb toweb = new ToWeb();
				toweb.setMsg("没有登录");
				toweb.setStatus(Status.NO_LOGIN); 
				return new Gson().toJson(toweb);
			}if(this.processurl==null)  {
				ToWeb toweb = new ToWeb();
				toweb.setMsg("没有权限");
				toweb.setStatus(Status.NO_PRIVILEGE);
				return new Gson().toJson(toweb);
			}if(this.usersessionFactory==null) {
				ToWeb toweb = new ToWeb();
				toweb.setMsg("没有数据权限");
				toweb.setStatus(Status.NO_PRIVILEGE);
				return new Gson().toJson(toweb);
			}*/
			obj	=	pjp.proceed(pjp.getArgs());
		}
		return obj;  
 	} 
	public Object login(String username,String passwor) {
		this.user	=	new User();
		this.processurl	=	new HashMap<>();
		
		return user;
	}
}
